<template>
  <div class="ele-body">
    <el-card shadow="never">
      <el-form :model="where" label-width="auto" ref="formRef" @keyup.enter.native="reload"
               @submit.native.prevent>
        <el-row :gutter='15'>
          <el-col :lg="6" :md="12">
            <el-form-item label="仓库名称：" prop="name">
              <el-input v-model="where.name" placeholder="请输入仓库名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <div class="ele-form-actions">
              <el-button class="ele-btn-icon" icon="el-icon-search" type="primary" @click="reload">
                查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>

      <!-- 数据表格 -->
      <ele-pro-table ref="table" :columns="columns" :datasource="url" :selection.sync="selection" :where="where"
                     height="calc(100vh - 325px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-radio-group v-model="where.is_manager" style="margin-right: 25px" @input="reload" size="small">
            <el-radio-button :label="0">全部</el-radio-button>
            <el-radio-button :label="1">我管理的</el-radio-button>
          </el-radio-group>

          <el-button v-if="permission.includes('sys:storehouse:add')" @click="onAdd" class="ele-btn-icon"
                     icon="el-icon-plus" size="small" type="primary">
            新建仓库
          </el-button>
        </template>

        <template slot="action" slot-scope="{row}">
          <el-link v-if="permission.includes('sys:storehouse:edit')&&checkAuth(row.users)" :underline="false"
                   icon="el-icon-edit"
                   type="primary" @click="onEdit(row)">
            编辑
          </el-link>
        </template>

        <template v-slot:user="{row}">
          {{ row.users.map(item => item.realname).join(',') }}
        </template>

        <template v-slot:depts="{row}">
          {{ row.depts.map(item => item.name).join(',') }}
        </template>

        <template v-slot:address="{row}">
          {{ row.province_name + row.city_name + row.county_name + row.address }}
        </template>
      </ele-pro-table>
    </el-card>
    <AddDialog ref="addDialog" @reload="reload"/>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import AddDialog from "@/views/bom/storehouse/components/add-dialog.vue";

export default {
  components: {
    AddDialog,
  },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/warehouses',
      where: {
        is_manager: 0,
        name: null,
      },
      id: JSON.parse(localStorage.getItem('user'))?.id,
      // 表格列配置
      columns: [
        // {
        //   columnKey: 'selection',
        //   type: 'selection',
        //   width: 45,
        //   align: 'center',
        //   fixed: "left"
        // },
        {
          prop: 'name',
          label: '仓库名称',
          minWidth: 200,
          align: 'center',
          showOverflowTooltip: true,
        },
        {
          slot: 'user',
          label: '仓库管理员',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          slot: 'depts',
          label: '部门限制',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          slot: 'address',
          label: '地址',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格选中数据
      selection: [],
    };
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.$refs.formRef.resetFields()
      this.where.is_manager = 0
      this.reload();
    },
    onAdd() {
      this.$refs.addDialog.visible = true
    },
    onEdit(row) {
      this.$refs.addDialog.onActive(row)
      this.$refs.addDialog.visible = true
    },
    checkAuth(users) {
      return users.some(({id}) => id == this.id)
    },
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-tag + .el-tag {
  margin-left: 6px;
}
</style>
